package models.repository;

import models.crawlers.common.SessionParams;
import models.crawlers.events.ProductProcessed;
import models.entities.CrawlerActionEntity;
import models.entities.CrawlerEntity;
import models.entities.CrawlerWarningEntity;
import models.exceptions.CreateEntityException;
import models.exceptions.GetEntityException;
import models.exceptions.UpdateEntityException;
import models.helpers.DBSession;
import models.repository.common.CrawlerRepository;
import mybatis.mappers.CrawlerMapper;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.List;

@Deprecated
public class CrawlerRepositorySQL implements CrawlerRepository {
    public SessionParams createNewSession(int userId, String crawlerName) throws CreateEntityException, IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            CrawlerMapper crawlerMapper = dbSession.getMapper(CrawlerMapper.class);
            SessionParams sessionParams = new SessionParams(-1, userId, crawlerName);
            //crawlerMapper.createNewSession(sessionParams);
            dbSession.commit();
            return sessionParams;
        } catch (Exception ex) {
            if (dbSession != null) {
                dbSession.rollback();
            }
            throw new CreateEntityException(MessageFormat.format("Cannot create session: {0}", ex.getMessage()));
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public void writeAction(CrawlerActionEntity crawlerAction) throws IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            CrawlerMapper crawlerMapper = dbSession.getMapper(CrawlerMapper.class);
            crawlerMapper.writeAction(crawlerAction);
            dbSession.commit();
        } catch (Exception ex) {
            //It's just the log activity, so hide error
            if (dbSession != null) {
                dbSession.rollback();
            }
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public void changeCategoryIndex(int sessionId, int categoryIndex) throws IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            CrawlerMapper crawlerMapper = dbSession.getMapper(CrawlerMapper.class);
            crawlerMapper.changeCategoryIndex(sessionId, categoryIndex);
            dbSession.commit();
        } catch (Exception ex) {
            //It's just the log activity, so hide error
            if (dbSession != null) {
                dbSession.rollback();
            }
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public void writeWarning(CrawlerWarningEntity warning) throws IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            CrawlerMapper crawlerMapper = dbSession.getMapper(CrawlerMapper.class);
            crawlerMapper.writeWarning(warning);
            dbSession.commit();
        } catch (Exception ex) {
            //It's just the log activity, so hide error
            if (dbSession != null) {
                dbSession.rollback();
            }
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public void setProductCount(CrawlerEntity crawlerInfo) throws IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            CrawlerMapper crawlerMapper = dbSession.getMapper(CrawlerMapper.class);
            crawlerMapper.setProductCount(crawlerInfo);
            dbSession.commit();
        } catch (Exception ex) {
            //It's just the log activity, so hide error
            if (dbSession != null) {
                dbSession.rollback();
            }
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public void setCrawlerPosition(ProductProcessed eventObject) throws IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            CrawlerMapper crawlerMapper = dbSession.getMapper(CrawlerMapper.class);
            crawlerMapper.setCrawlerPosition(eventObject);
            dbSession.commit();
        } catch (Exception ex) {
            //It's just the log activity, so hide error
            if (dbSession != null) {
                dbSession.rollback();
            }
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public void updateSession(SessionParams sessionParams) throws IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            CrawlerMapper crawlerMapper = dbSession.getMapper(CrawlerMapper.class);
            crawlerMapper.updateSession(sessionParams);
            dbSession.commit();
        } catch (Exception ex) {
            //It's just the log activity, so hide error
            if (dbSession != null) {
                dbSession.rollback();
            }
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public CrawlerEntity getCrawlerInfo(String crawlerName) throws GetEntityException, IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            CrawlerMapper crawlerMapper = dbSession.getMapper(CrawlerMapper.class);
            return crawlerMapper.getCrawlerInfo(crawlerName);
        } catch (Exception e) {
            throw new GetEntityException("GetCrawlerInfo");
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public void updateCrawlerInfo(CrawlerEntity crawlerInfo) throws UpdateEntityException, IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            CrawlerMapper crawlerMapper = dbSession.getMapper(CrawlerMapper.class);
            crawlerMapper.updateCrawlerInfo(crawlerInfo);
            dbSession.commit();
        } catch (Exception ex) {
            if (dbSession != null) {
                dbSession.rollback();
            }
            throw new UpdateEntityException("updateCrawlerInfo");
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }


    public void changePageNumber(int sessionId, int pageNumber) throws IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            CrawlerMapper crawlerMapper = dbSession.getMapper(CrawlerMapper.class);
            crawlerMapper.changePageNumber(sessionId, pageNumber);
            dbSession.commit();
        } catch (Exception ex) {
            //It's just the log activity, so hide error
            if (dbSession != null) {
                dbSession.rollback();
            }
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public List<CrawlerEntity> getAllCrawlers() throws GetEntityException, IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            CrawlerMapper crawlerMapper = dbSession.getMapper(CrawlerMapper.class);
            return crawlerMapper.getAllCrawlers();
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public List<SessionParams> getSessions(String crawlerTitle) throws IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            CrawlerMapper crawlerMapper = dbSession.getMapper(CrawlerMapper.class);
            return crawlerMapper.getSessions(crawlerTitle);
        }
        finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public SessionParams getSession(int sessionId) throws IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            CrawlerMapper crawlerMapper = dbSession.getMapper(CrawlerMapper.class);
            return crawlerMapper.getSession(sessionId);
        }
        finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }
}
